import machine
import time

@micropython.native
def mark(pin, us: int):
    delay_count = 5 # 5 or 6, from test for esp8266
    #print('mark', us)
    count = int(us*38/1000) # us / 26
    for i in range(count):
        #time.sleep_us(0)
        for i in range(delay_count):
            pass
        #time.ticks_us()
        #pass
        pin.on()
        #pin.value(1)
        #time.sleep_us(0)
        for i in range(delay_count):
            pass

        pin.off()
        #pin.value(0)

@micropython.native
def space(pin, us: int):
    #print('space', us)
    pin.off()
    time.sleep_us(us)

    
@micropython.native
def send_raw(pin, arr):
    print(sum(arr))
    #pin = machine.Pin(2, machine.Pin.OUT)
    isr = machine.disable_irq()
    freq0 = machine.freq()
    machine.freq(160000000)
    t1 = time.ticks_us()
    b_mark = True
    for us in arr:
        #print(action, x)
        if b_mark:
            mark(pin, us)
        else:
            space(pin, us)
        b_mark = not b_mark
            
    #pin.on()

    t2 = time.ticks_us()
    machine.enable_irq(isr)
    machine.freq(freq0)

    return t2-t1

def gree_on():
    data = [
        8900, 4450,
        650, 550, 650, 1600, 700, 550, 650, 1600, 650, 1650, 650, 550, 650, 550, 650, 550,
        650, 550, 650, 500, 700, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550,
        650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550,
        650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500,
        700, 500, 650, 1650, 650, 550,
        650, 19750,
        700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,
        650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 1650,
        650, 39600, 8900, 4400,
        700, 500, 700, 1600, 650, 550, 650, 1650, 650, 1600, 700, 550, 650, 500, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 1600, 700, 550, 650, 550, 650, 500, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 1600, 650, 1650, 650, 550,
        650, 550, 650, 1600, 650, 550,
        650, 19800,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 500, 650, 550,
        650, 550, 650, 550, 650, 550, 650, 550, 650, 1600, 700, 500, 700, 1600, 650, 1650,
        650]

    pin = machine.Pin(4, machine.Pin.OUT)
    print(send_raw(pin, data))
    
def gree_off():
    data = [8950, 4450,
    650, 550, 650, 1600, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 500, 700, 500, 650, 1650, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 1650, 650, 550, 650, 550,
    650, 500, 700, 500, 700, 500, 650, 550, 650, 1650, 650, 550, 650, 1600, 700, 500,
    650, 550, 650, 1650, 650, 550,
    650, 19750,
    700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 550, 650, 550, 650, 500, 700, 500, 700, 1600, 650, 550,
    650, 39600, 8900, 4450,
    650, 550, 650, 1600, 700, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 500, 700, 500, 700, 500, 650, 1650, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 500, 700, 500, 650, 1650, 650, 1650, 650, 1600, 700, 500,
    650, 550, 650, 1650, 650, 550,
    650, 19750,
    700, 500, 700, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 500, 700, 500, 650, 550, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 550, 650, 550,
    650, 550, 650, 550, 650, 550, 650, 500, 700, 1600, 650, 550, 650, 1650, 650, 550,
    650]

    pin = machine.Pin(4, machine.Pin.OUT)
    print(send_raw(pin, data))   

    
    
if __name__ == '__main__':
#     gree_on()
#     time.sleep(3)
    gree_off()
    




    